from django.shortcuts import render
from django.views import View
from django.http import HttpResponse
from django.utils.decorators import method_decorator


# Create your views here.

# url(r'^template_view/$', views.TemplateView.as_view()),
class TemplateView(View):

    def get(self, request):
        context = {
            "name": "cg",
            "alist": [2, 3, 4],
            "adict": {"age": "20"}
        }
        return render(request, 'index.html', context=context)


def my_decorator(view_func):
    """装饰器"""

    def wrapper(request, *args, **kwargs):
        print("我的装饰器生效了")
        return view_func(request, *args, **kwargs)


    return wrapper


# @my_decorator
# def test(request):
#     return HttpResponse('ok')

# 可以只装类视图中的指定方法
# url(r'^demoview/$', views.DemoView.as_view()),
@method_decorator(my_decorator, 'get')
class DemoView(View):
    """定义类视图"""
    # @my_decorator
    def get(self, request):
        return HttpResponse('get请求业务逻辑')

    def post(self, request):
        return HttpResponse('post请求业务逻辑')
